import Storage from "./storage"

const cacheState = {
  'REFRESH_SIGN': {
    value: null,
    storage: true,
  },
  'CONFIG_INFO': {
    value: null,
    storage: true,
  },
  'LOGIN_INFO': {
    value: null,
    storage: true,
  },
  'USER_INFO': {
    value: null,
    storage: true,
  },
  'PRODUCT_INFO': {
    value: null,
    storage: true,
  },
}
const state = new Proxy(cacheState, {
  get(target, key) {
    const { value, storage } = target[key]
    if (value) return value
    if (storage) return Storage.get(`m:${key}`)
    return null
  },
  set(target, key, value) {
    Reflect.set(target[key], 'value', value)
    if (value === null) {
      Storage.remove(`m:${key}`)
      return true
    }
    const { storage } = target[key]
    if (storage) Storage.set(`m:${key}`, value)
    return true
  },
})

export default state